package display.threads;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

import structs.adoption.Adoption;
import structs.book.Book;
import structs.term.dept.Department;
import structs.term.dept.Department.Code;
import structs.term.dept.course.Course;
import structs.term.dept.course.section.Instructor;
import structs.term.dept.course.section.Section;
import Util.Util;
import display.InventoryData;
import exception.InvalidNameException;

public class AdoptionThread extends MyThread {
	private File f;
	private String deptName, dept, course, section, credits, courseName,
				   capacity, enrollment, inst, track, authorF, titleF,
				   authorM, titleM, isbnF, isbnM, ed, status, publisher,
				   notes, prevEnroll, qtyNeed, estBB, comtext,
				   newPrice, usedPrice, invNum, bcodeU, bcodeN;
	private int crs;
	
	public AdoptionThread(File fin, InventoryData data) {
		super(data,"Adoption Import",false);
		f = fin;
		setStatus("initializing");
	}

	/**
	 * Parses the input file and saves the adoptions in the InventoryData object.
	 * @param books
	 */
	@Override
	public void myRun() {
		setStatus("parsing");
		Scanner sc;
		try {
			//int count = 0;
			sc = new Scanner(new FileInputStream(f)).useDelimiter("\t|\n");
		
			while (sc.hasNext()) {
				
				readLine(sc);
				
				Department d;
				Course c;
				Section s;
				Instructor in;
				Book b;
				
				//check dept
				Code dcode = Department.Code.getCode(dept);
				if (dcode == null)
					Util.printErrorString("Unknown Department Code: "+dept);
				
				d = inv.getCurTerm().getDept(dcode);
				
				//check course
				c = d.getCourse(Integer.valueOf(course));
				
				if (c == null)
					c = d.addCourse(Integer.valueOf(course),courseName);
				
				//check inst / section
				in = Instructor.get(inst);
				s = c.getSection(section);
				
				if (s == null)
					s = c.addSection(section, in);
				
				//check book
				
				if (isbnF.equalsIgnoreCase("LM"))
					b = Book.LM;
				else if (isbnF.equalsIgnoreCase("NT"))
					b = Book.NT;
				else if (isbnF.equalsIgnoreCase("NA"))
					b = Book.NA;
				else {
					b = inv.getBook(invNum);
					//System.out.println("book already exists");
					if (b == null)
						b = new Book(clean(authorF), clean(titleF), Long.valueOf(isbnF),clean(ed),clean(invNum));
						
						//id.setCount(f.getName(), ++count);
				}
				//add adoption
				
				if (d == null)
					System.out.println("null dept");
				if (c == null)
					System.out.println("null course");
				if (s == null)
					System.out.println("null section");
				if (b == null)
					System.out.println("null book");
				
				
				Adoption a = new Adoption(s,b,status);
				//adoptions.add(a);
				//System.out.println(a.toString());
				s.addAdoption(a);
				inv.addAdoption(a);
				inv.addBook(b);
				Util.printStatusString("adopted book: "+b.getInv_num()+" for course: "+s.getSec_id());
			}
			
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (InvalidNameException e) {
			e.printStackTrace();
		}
		Util.printStatusString("parsing finished");
		setStatus("finished");
		
		//return adoptions;
	}

	private void readLine(Scanner sc) {
		deptName = sc.next();
		dept = sc.next();
		course = sc.next(); crs = Integer.valueOf(course);
		section = sc.next();
		credits = sc.next();
		courseName = sc.next();
		capacity = sc.next();
		enrollment = sc.next();
		inst = sc.next();
		track = sc.next();
		authorF = sc.next();
		titleF = sc.next();
		authorM = sc.next();
		titleM = sc.next();
		isbnF = sc.next();
		isbnM = sc.next();
		ed = sc.next();
		status = sc.next();
		publisher = sc.next();
		notes = sc.next();
		prevEnroll = sc.next();
		qtyNeed = sc.next();
		estBB = sc.next();
		comtext = sc.next();
		newPrice = sc.next();
		usedPrice = sc.next();
		invNum = sc.next();
		bcodeU = sc.next();
		bcodeN = sc.next();
	}
	
	/**
	 * Removes single quotes (') from the given string.
	 * @param s
	 * @return
	 */
	private String clean(String s) {
		return s.replace('\'', ' ').replace('\\',' ');
	}
}
